<?php
// +----------------------------------------------------------------------
// | Citrix [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.tpCitrix.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.tpCitrix.com/ )
// +----------------------------------------------------------------------
// | Author: Citrix < 2722279500@qq.com >
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\controller\Permissions;
use \think\Db;
use \think\Cookie;
use \think\Session;
use \think\Cache;
use app\admin\model\Nav as NavModel;

class Nav extends Permissions
{
	//导航
	public function index()
	{
        $type = input('type/d',1);
        $model = new NavModel();
        $nav = $model->where(['type'=>$type])->order('sort desc')->select();
        $nav = $model->catelist($nav);
        $this->assign('type',$type);
        $this->assign('nav',$nav);
		return $this->fetch();
	}
	//编辑（新增）
	public function publish()
	{
		//获取菜单id
    	$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    	$model = new NavModel();

        if(!empty($id))
        {
            $type = Db::name('nav')->where('id',$id)->value('type');
            $nav = $model->where('type',$type)->order('sort desc')->select();
        }else
        {
            $nav = $model->order('sort desc')->select();
        }
            $this->assign('cates',$model->catelist($nav));
        
		//是正常添加操作
		if($id > 0) {
    		//是修改操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
	            //验证菜单是否存在
	            $link = $model->where('id',$id)->find();
	            if(empty($link)) {
	            	return $this->error('id不正确');
	            }
                //设置修改人
                $post['edit_admin_id'] = Session::get('admin');
	            if(false == $model->allowField(true)->save($post,['id'=>$id])) {
	            	return $this->error('修改失败');
	            } else {
                    addlog($model->id);//写入日志
	            	return $this->success('修改成功','admin/nav/index');
	            }
    		} else {
    			//非提交操作
    			$nav = $model->where('id',$id)->find();
                if(!empty($nav)) {
                    $this->assign('info',$nav);
                    return $this->fetch();
                } else {
                    return $this->error('id不正确');
                }
    		}
    	} else {
    		//是新增操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
	            if(false == $model->allowField(true)->save($post)) {
	            	return $this->error('添加失败');
	            } else {
                    addlog($model->id);//写入日志
	            	return $this->success('添加成功','admin/nav/index');
	            }
    		} else {
    			//非提交操作
    			return $this->fetch();
    		}
    	}
	}
	//删除
	public function delete()
    {
    	if($this->request->isAjax()) {
    		$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            Db::name('show')->where('nav_id',$id)->delete();
            if(false == Db::name('nav')->where('id',$id)->delete()) {
                return $this->error('删除失败');
            } else {
                addlog($id);//写入日志
                return $this->success('删除成功','admin/nav/index');
            }
    	}
    }
	
}




?>